<template>
    <el-button
        :size="size"
        :type="type"
        :icon="icon"
        :nativeType="nativeType"
        :loading="loading"
        :disabled="!!(disabled)"
        :plain="plain"
        :autofocus="autofocus"
        :round="round"
        :circle="circle"
        @click="handleClickBefore"
    >
        <slot></slot>
    </el-button>
</template>

<script>
import { strIsEmpty,arrayIsEmpty,isNullOrUndefined } from '../../core.js'

export default {
    inject: {
        elForm: {
            default: ''
        },
        elFormItem: {
            default: ''
        }
    },
    name: "SksButton",
    props:{
        /**
         *
         <el-button type="primary">主要按钮</el-button>
         <el-button type="success">成功按钮</el-button>
         <el-button type="info">信息按钮</el-button>
         <el-button type="warning">警告按钮</el-button>
         <el-button type="danger">危险按钮</el-button>
         */
        type: String,
        size: {
            type: String,
            //small/mini
            default: undefined,
        },
        icon: String,
        nativeType: String,
        loading: Boolean,
        disabled: [Boolean,Number],
        plain: Boolean,
        autofocus: Boolean,
        round: Boolean,
        circle: Boolean,

        /**
         * 点击前远程检查是否可操作
         * 请求
         ()=>{
             return axios({
                url: '',
                method: 'post',
                data: {}
            })
         }
         */
        clickBeforeCheckRemoteFunc: Function,

        //提示信息,如果为空则不提示
        confirmMsg: [String,Function],
    },
    methods:{
        //点击前检查是否可操作
        handleClickBefore(e){
            if(isNullOrUndefined(this.clickBeforeCheckRemoteFunc)){
                this.emitClickIfConfirmMsg(e)
                return;
            }

            this.clickBeforeCheckRemoteFunc().then(res=>{
                this.emitClickIfConfirmMsg(e)
            })
        },
        emitClickIfConfirmMsg(e){
            if (isNullOrUndefined(this.confirmMsg)) {
                this.$emit('click',e)
                return;
            }
            //检查无错误提示,检查是否需要提示
            let msg
            if(typeof this.confirmMsg=='string'){
                msg=this.confirmMsg
            }else{
                msg=this.confirmMsg()
            }
            if (strIsEmpty(msg)) {
                this.$emit('click',e)
                return;
            }
            this.$confirm(msg, "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() =>{
                this.$emit('click',e)
            })

        },
    },
}
</script>

<style scoped>

</style>

<!--
                    <sks-button
                        type="warning"
                        icon="el-icon-download"
                        :disabled="!safeGet(mainTable,`currentRow.${mainTable.uniqueId}`)"
                        @click="finishProduction(mainTable.currentRow)"
                        :clickBeforeCheckRemoteFunc="()=> request({
                            url: '/business/hello/deleteCheck',
                            method: 'post',
                            data: safeGet(mainTable,`currentRow`)
                          })"

                    >完结</sks-button>
-->
